package com.feihao.zhou.test;

import com.feihao.zhou.bean.*;
import com.feihao.zhou.mapper.ManyTableSelect;
import com.feihao.zhou.mapper.SingleTableCRUD;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.List;

public class test {

    //单表查询所有学生
    @Test
    public void test01() {
        SqlSession sqlSession = DBUtils.getSqlSession();
        SingleTableCRUD mapper = sqlSession.getMapper(SingleTableCRUD.class);
        List<Student> list = mapper.selectAllStudent();
        for (Student student : list) {
            System.out.println(student);
        }
        sqlSession.close();
    }

    @Test
    public void test02() {
        SqlSession sqlSession = DBUtils.getSqlSession();
        SingleTableCRUD mapper = sqlSession.getMapper(SingleTableCRUD.class);
        Student student1 = mapper.selectStudentById(2);
        System.out.println(student1);
        sqlSession.close();
    }

    @Test
    public void test03() {
        SqlSession sqlSession = DBUtils.getSqlSession();
        SingleTableCRUD mapper = sqlSession.getMapper(SingleTableCRUD.class);
        List<Student> students = mapper.selectStudentByIdOrAge(6, 28);
        System.out.println(students);
        sqlSession.close();
    }

    @Test
    public void test04() {
        SqlSession sqlSession = DBUtils.getSqlSession();
        SingleTableCRUD mapper = sqlSession.getMapper(SingleTableCRUD.class);
        Integer integer = mapper.insertStudent(new Student(null, "bbjj", 28, 2));

        //提交事务：
        sqlSession.commit();
        System.out.println(integer);
        sqlSession.close();
    }

    @Test
    public void test05() {
        SqlSession sqlSession = DBUtils.getSqlSession();
        SingleTableCRUD mapper = sqlSession.getMapper(SingleTableCRUD.class);
        Integer i = mapper.updateStudent("八八九九", 8899, 14);

        //提交事务：
        sqlSession.commit();
        System.out.println(i);
        sqlSession.close();
    }

    @Test
    public void test06() {
        SqlSession sqlSession = DBUtils.getSqlSession();
        SingleTableCRUD mapper = sqlSession.getMapper(SingleTableCRUD.class);
        Integer integer = mapper.deleteStudent(14);

        //提交事务：
        sqlSession.commit();
        System.out.println(integer);
        sqlSession.close();

    }

    /*----------------------------------多表查询----------------------------------------*/

    @Test
    public void test07() {
        SqlSession sqlSession = DBUtils.getSqlSession();
        ManyTableSelect mapper = sqlSession.getMapper(ManyTableSelect.class);
        List<Card> cards = mapper.selectAllCard();
        for (Card card : cards) {
            System.out.println(card.getId() + "," + card.getNumber());
            Person person = card.getPerson();
            System.out.println(person);
        }
        sqlSession.close();
    }

    @Test
    public void test08() {
        SqlSession sqlSession = DBUtils.getSqlSession();
        ManyTableSelect mapper = sqlSession.getMapper(ManyTableSelect.class);
        List<Classes> classes = mapper.selectAllClasses();
        for (Classes aClass : classes) {
            System.out.println(aClass.getId() + "," + aClass.getName());
            List<Student> students = aClass.getStudents();
            for (Student student : students) {
                System.out.println("\t" + student);
            }
        }
        sqlSession.close();
    }

    @Test
    public void test09() {
        SqlSession sqlSession = DBUtils.getSqlSession();
        ManyTableSelect mapper = sqlSession.getMapper(ManyTableSelect.class);
        List<Student_Course> student_courses = mapper.selectAllStudent_Course();
        for (Student_Course student_cours : student_courses) {
            System.out.println(student_cours);
            List<Course> course = student_cours.getCourse();
            for (Course course1 : course) {
                System.out.println(course1);
            }
        }
    }
}
